/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__resp_data_analysis.c                            */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/30 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <pop3/pop3.h>
#include "frsutl.h"
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frreport_comm.h"
#include "../../../src/module/pop3/config.h"

extern ApmContext g_st_apm_context;
extern void *g_p_apm_context;

typedef struct __st_L7session_pop3_private
{
    unsigned long ul_seq;                             //序列号，1次req和response时，使用同一个序列号
    L4_transaction st_l4_trans;                       //数据
    L7_transaction st_l7_trans;                       //数据
    int i_flag_data_type;                             //1: FRLOGIN_START 2:FRLOGIN_SUCC 3:FRLOGIN_FAIL 4 FRLOGOUT_START 5 FRLOGOUT_END 0 : other
    char *p_header_name;
    Frreport_body_data *p_st_body_data_req;
    Frreport_body_data *p_st_body_data_resp;
    Frlist_header *p_lst_comm;
    Frlist_header *p_lst_req;
    char *p_user_name;
    struct timeval st_login_timestamp;
    char arr_session_id[APM_UUID_SIZE];
    int i_sign;                                       //标志位 0||1, 0:需要resp解析 1:不需要resp解析
    int i_flag_end;                                   //标志位 0||1, 0:req为PASS 1:req不为PASS
    int i_flag_response;                              //标志位 0||1, 0:response的code和string不必上报 1：需要上报
}L7session_pop3_private;

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

extern L7session_pop3_private* __pop3_private_create(void);
extern int __resp_data_analysis(Conntrack *p_st_conn
                                , Apm_packet *p_st_data_in
                                , Frhash_header *p_st_hash_report
                                , L7_session *p_st_session);
extern int pop3_session_destroy(void *p_private_session);
extern void list_report_node_free(Frlist_node *p_st_node);

static void case1(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    Frhash_header *p_st_hash_report = NULL;
    L7_session *p_st_session = NULL;

    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, p_st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    CU_ASSERT_PTR_EQUAL(p_st_session, NULL);

    return;
}

static void case2(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 3), FR_SUCC);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("+OK 1 56519\r\n");
    if(p_st_data_in->p_payload == NULL)
        goto ERROR;
    p_st_data_in->ui_payload_len = 0;
    p_st_data_in->st_timestamp.tv_sec = 10;
    p_st_data_in->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __pop3_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;

    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_response = 1;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type = 2;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec = 6;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec = 25;

    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign, 0);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type, FRLOGIN_SUCC);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 0);

    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us, 0);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 0);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 0);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    frhash2_free(&st_hash_report, list_report_node_free);
    pop3_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }

    return;
}

static void case3(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 3), FR_SUCC);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("+OK 1 56519\r\n");
    if(p_st_data_in->p_payload == NULL)
        goto ERROR;
    p_st_data_in->ui_payload_len = 13;
    p_st_data_in->st_timestamp.tv_sec = 10;
    p_st_data_in->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __pop3_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;

    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_response = 1;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type = 2;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec = 6;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec = 25;

    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign, 1);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type, 2);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 0);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    frhash2_free(&st_hash_report, list_report_node_free);
    pop3_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }

    return;
}

static void case4(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 3), FR_SUCC);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("+OK 1 56519\r\n");
    if(p_st_data_in->p_payload == NULL)
        goto ERROR;
    p_st_data_in->ui_payload_len = 13;
    p_st_data_in->st_timestamp.tv_sec = 10;
    p_st_data_in->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __pop3_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->p_st_body_data_resp = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(((L7session_pop3_private *)(p_st_session->p_session_private))->p_st_body_data_resp == NULL)
        goto ERROR;
    memset(((L7session_pop3_private *)(p_st_session->p_session_private))->p_st_body_data_resp, 0x00, sizeof(Frreport_body_data));
    ((L7session_pop3_private *)(p_st_session->p_session_private))->p_st_body_data_resp->em_type = FRREPORT_TYPE_L7DATA;

    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_response = 1;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type = 2;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 2;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 25;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec = 0;

    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign, 1);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type, 2);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 2);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 0);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    frhash2_free(&st_hash_report, list_report_node_free);
    pop3_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }

    return;
}

static void case5(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 3), FR_SUCC);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("+OK Name is a valid mailbox\r\n");
    if(p_st_data_in->p_payload == NULL)
        goto ERROR;
    p_st_data_in->ui_payload_len = 29;
    p_st_data_in->st_timestamp.tv_sec = 10;
    p_st_data_in->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __pop3_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;

    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_response = 1;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type = 1;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec = 0;

    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign, 0);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type, 1);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 0);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us, 0);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    frhash2_free(&st_hash_report, list_report_node_free);
    pop3_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }

    return;
}

static void case6(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 3), FR_SUCC);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("+OK mail.broada.com Cyrus POP3 git2.5+0-Kolab-2.5-48.5.el6.kolab_3.3 server ready\r\n");
    if(p_st_data_in->p_payload == NULL)
        goto ERROR;
    p_st_data_in->ui_payload_len = 83;
    p_st_data_in->st_timestamp.tv_sec = 10;
    p_st_data_in->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __pop3_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;

    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_response = 1;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec = 0;

    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign, 0);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type, 0);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 0);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_firstbyte_us, 0);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us, 0);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 0);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 0);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 0);
    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 0);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    frhash2_free(&st_hash_report, list_report_node_free);
    pop3_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }

    return;
}

static void case7(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 3), FR_SUCC);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("+OK Mailbox locked and ready SESSIONID=<mail.broada.com-24121-1427942444-1>\r\n");
    if(p_st_data_in->p_payload == NULL)
        goto ERROR;
    p_st_data_in->ui_payload_len = 77;
    p_st_data_in->st_timestamp.tv_sec = 10;
    p_st_data_in->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __pop3_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;

    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_end = 1;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_response = 1;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type = 1;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec = 6;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec = 25;

    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign, 1);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type, 2);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 0);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    frhash2_free(&st_hash_report, list_report_node_free);
    pop3_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }

    return;
}

static void case8(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 3), FR_SUCC);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("+OK\r\n");
    if(p_st_data_in->p_payload == NULL)
        goto ERROR;
    p_st_data_in->ui_payload_len = 5;
    p_st_data_in->st_timestamp.tv_sec = 10;
    p_st_data_in->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __pop3_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;

    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_response = 1;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type = 2;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec = 6;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec = 25;

    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign, 1);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type, 5);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 0);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    frhash2_free(&st_hash_report, list_report_node_free);
    pop3_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }

    return;
}

static void case9(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 3), FR_SUCC);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("+OK scan listing follows\r\n");
    if(p_st_data_in->p_payload == NULL)
        goto ERROR;
    p_st_data_in->ui_payload_len = 30;
    p_st_data_in->st_timestamp.tv_sec = 10;
    p_st_data_in->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __pop3_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;

    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_response = 1;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type = 2;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec = 6;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec = 25;

    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign, 1);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type, 2);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 0);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    frhash2_free(&st_hash_report, list_report_node_free);
    pop3_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }

    return;
}

static void case10(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_data_in = NULL;
    L7_session *p_st_session = NULL;
    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 3), FR_SUCC);

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_data_in = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_data_in == NULL)
        goto ERROR;
    memset(p_st_data_in, 0x00, sizeof(Apm_packet));
    p_st_data_in->p_payload = frstrdup("1 56519\r\n.\r\n");
    if(p_st_data_in->p_payload == NULL)
        goto ERROR;
    p_st_data_in->ui_payload_len = 12;
    p_st_data_in->st_timestamp.tv_sec = 10;
    p_st_data_in->st_timestamp.tv_usec = 25;

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    if(p_st_session == NULL)
        goto ERROR;
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __pop3_private_create();
    if(p_st_session->p_session_private == NULL)
        goto ERROR;

    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign = 1;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_response = 1;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type = 2;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec = 0;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec = 6;
    ((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec = 25;

    i_ret = __resp_data_analysis(p_st_conn, p_st_data_in, &st_hash_report, p_st_session);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_sign, 1);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type, 2);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_response_bytes, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.ul_response_time_us, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_sec, 10);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time_first_byte.tv_usec, 25);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_sec, 0);
//    CU_ASSERT_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_response_time.tv_usec, 0);

    frfree(p_st_conn);
    frfree(p_st_data_in->p_payload);
    frfree(p_st_data_in);
    frhash2_free(&st_hash_report, list_report_node_free);
    pop3_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_data_in)
    {
        if(p_st_data_in->p_payload)
            frfree(p_st_data_in->p_payload);
        frfree(p_st_data_in);
    }
    frhash2_free(&st_hash_report, list_report_node_free);
    if(p_st_session)
    {
        if(p_st_session->p_session_private)
            frfree(p_st_session->p_session_private);
        frfree(p_st_session);
    }

    return;
}

int add_suite__resp_data_analysis(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __resp_data_analysis()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL ||
        CU_add_test(pSuite, "case6", case6) == NULL ||
        CU_add_test(pSuite, "case7", case7) == NULL ||
        CU_add_test(pSuite, "case8", case8) == NULL ||
        CU_add_test(pSuite, "case9", case9) == NULL ||
        CU_add_test(pSuite, "case10", case10) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
